<?php

//*******************************************************
//* JAccelerator Component
//* http://fun.kubera.org
//* (C) Radoslaw Kubera
//* license http://www.gnu.org/copyleft/gpl.html GNU/GPL
//*******************************************************

/** ensure this file is being included by a parent file */
defined('_VALID_MOS') or die();

// ensure user has access to this function
if(!($acl->acl_check('administration', 'edit', 'users', $my->usertype, 'components', 'all') | $acl->acl_check('administration', 'edit', 'users', $my->usertype, 'components', 'com_contact'))) {
	mosRedirect('index2.php', _NOT_AUTH);
}

ini_set('max_execution_time', 0);

global $mosConfig_absolute_path, $database;
global $mosConfig_absolute_path;

include_once ($mosConfig_absolute_path."/components/com_joomlacache/joomlacache.php");

$adminPath = $mosConfig_absolute_path.'/administrator';

include_once ("$adminPath/components/com_joomlacache/admin.joomlacache.html.php");

if(file_exists($mosConfig_absolute_path."/administrator/components/com_joomlacache/language/$mosConfig_lang.php")) {
	include_once ($mosConfig_absolute_path."/administrator/components/com_joomlacache/language/$mosConfig_lang.php");
} else {
	include_once ($mosConfig_absolute_path."/administrator/components/com_joomlacache/language/english.php");
}

$task = mosGetParam($_REQUEST, 'task', '');
foreach ($_REQUEST as $key => $value) {
	if(eregi('module_clear_', $key)) {
		$task = 'clear_module_cache';
		if($key == 'clear_all')
			$clear_option = '';
		else
			$clear_option = str_replace('module_clear_', '', $key);
	} else
		if(eregi('clear_', $key)) {
			$task = 'clear_cache';
			if($key == 'clear_all')
				$clear_option = '';
			else
				$clear_option = str_replace('clear_', '', $key);
		}
}

$remove_option = mosGetParam($_REQUEST, 'remove_option', '');
$path = $mosConfig_absolute_path.'/components/com_joomlacache/cache/';

switch ($task) {
	case 'remove_cache':
		remove_cache($path.'/components/'.$remove_option.'/', $remove_option);
		break;
	case 'remove_module_cache':
		remove_module_cache($path.'/modules/'.$remove_option.'/', $remove_option);
		break;

	case 'clear_cache':
echo $clear_option;
		clear_cache($clear_option);
echo 2;
		break;
	case 'clear_module_cache':
		clear_module_cache($clear_option);
		break;

	case 'components':
		components();
		break;
	case 'modules':
		modules();
		break;
	case 'save_components':
		save_components();
		break;
	case 'save_modules':
		save_modules();
		break;
	case 'save_config':
		save_config();
		break;
	case 'chpub':
		chpub();
		break;
	case 'about':
		about();
		break;

	default:
		config();
		break;
}

function about() {
	HTML_joomlacache_admin::about();
}

function clear_cache($clear_option) {
	JoomlaCacheFlush($clear_option);
	mosRedirect('index2.php?option=com_joomlacache&task=components');
}

function clear_module_cache($clear_option) {
	JoomlaCacheFlushModules($clear_option);
	mosRedirect('index2.php?option=com_joomlacache&task=modules');
}

function save_config() {
	global $mosConfig_absolute_path;

	$params = mosGetParam($_REQUEST, 'params', array());
	$time = ($params['minutes'] * 60) + ($params['hours'] * 3600) + ($params['days'] * 86400);

	unset($params['days']);
	unset($params['hours']);
	unset($params['minutes']);

	$path = $mosConfig_absolute_path.'/components/com_joomlacache/config.php';

	$config = "<?php\n";
	$config .= "$"."params['default_cache_time']='".$time."';\n";

	foreach ($params as $key => $value) {
		$config .= "$"."params['".$key."']='".$value."';\n";
	}
	$config .= "?>";

	ob_start();
	$file = fopen($path, "w+");
	fwrite($file, $config);
	fclose($file);
	ob_end_flush();

	mosRedirect('index2.php?option=com_joomlacache&task=config');
}

function save_components() {
	global $mosConfig_absolute_path;

	$path = $mosConfig_absolute_path.'/components/com_joomlacache/components.php';
	if(file_exists($path))
		include $path;

	$content->name = _JOOMLACACHE_CONTENT;
	$content->option = 'com_content';

	$contents = array();
	$contents[] = $content;

	global $database;
	$database->setQuery("select * from #__components where link<>''");
	$components = $database->loadObjectList();

	$allcomponents = array_merge($contents, $components);

	$config = "<?php";
	foreach ($allcomponents as $component) {
		echo $component->option;
		$params = mosGetParam($_REQUEST, $component->option, array());

		$comp = $component->option;

		$time = ($params['minutes'] * 60) + ($params['hours'] * 3600) + ($params['days'] * 86400);

		if(isset($$comp)) {
			if($$comp->cache != $params['cache'] || $time == 0) {
				$path_cache = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$component->option.'/';
				JoomlaCacheDeleteDirectory($path_cache);
				JommlaCacheDeleteFromDB($component->option);
			}
		}

		if($time == 0)
			$params['cache'] = 0;
		$config .= "\n";
		$config .= '$'.$component->option.'->cache='.$params['cache'].";\n";
		$config .= '$'.$component->option.'->time='.$time.";\n";
	}
	$config .= '?>';
	ob_start();
	$file = fopen($path, "w+");
	fwrite($file, $config);
	fclose($file);
	ob_end_flush();
	mosRedirect('index2.php?option=com_joomlacache&task=components');
}

function save_modules() {
	global $mosConfig_absolute_path;

	$path = $mosConfig_absolute_path.'/components/com_joomlacache/modules.php';
	if(file_exists($path))
		include $path;

	global $database;
	$database->setQuery("select * from #__modules where access<'99'");
	$modules = $database->loadObjectList();

	$config = "<?php";
	foreach ($modules as $module) {
		$params = mosGetParam($_REQUEST, 'id_'.$module->id, array());

		$mod = 'id_'.$module->id;

		$time = ($params['minutes'] * 60) + ($params['hours'] * 3600) + ($params['days'] * 86400);
		if(isset($$mod)) {
			if($$mod->cache != $params['cache'] || $time == 0) {
				$path_cache = $mosConfig_absolute_path.'/components/com_joomlacache/cache/modules/'.$module->id.'/';
				JoomlaCacheDeleteDirectory($path_cache);
				JommlaCacheDeleteFromDB($module->id);
			}
		}

		if($time == 0)
			$params['cache'] = 0;
		$config .= "\n";
		$config .= '$'.'id_'.$module->id.'->cache='.$params['cache'].";\n";
		$config .= '$'.'id_'.$module->id.'->time='.$time.";\n";
	}
	$config .= '?>';
	ob_start();
	$file = fopen($path, "w+");
	fwrite($file, $config);
	fclose($file);
	ob_end_flush();

	mosRedirect('index2.php?option=com_joomlacache&task=modules');
}

function remove_cache($dir, $option) {
	JoomlaCacheDeleteDirectory($dir);
	if($option)
		JommlaCacheDeleteFromDB($option);
	else
		JommlaCacheDeleteFromDB('allcomponents');
	global $mosConfig_live_site;
	mosRedirect($mosConfig_live_site.'/administrator/index2.php?option=com_joomlacache&task=components');
	exit;
}

function remove_module_cache($dir, $option) {
	JoomlaCacheDeleteDirectory($dir);
	if($option)
		JommlaCacheDeleteFromDB($option);
	else
		JommlaCacheDeleteFromDB('allmodules');
	global $mosConfig_live_site;
	echo $option;
	mosRedirect($mosConfig_live_site.'/administrator/index2.php?option=com_joomlacache&task=modules');
	exit;
}

function chpub() {
	global $database;

	$database->setQuery("select * from #__plugins where element='joomlacache'");
	$database->loadObject($element);

	if($element) {
		if($element->published == 0)
			$published = 1;
		else
			$published = 0;
		$database->setQuery("update #__plugins set published='$published' where element='joomlacache'");
		$database->query();
	} else {
		$database->setQuery("select * from #__mambots where element='joomlacache'");
		$database->loadObject($element);
		if($element) {
			if($element->published == 0)
				$published = 1;
			else
				$published = 0;
			$database->setQuery("update #__mambots set published='$published' where element='joomlacache'");
			$database->query();
		} else
			install_plugin();
	}
	install_plugin();

	mosRedirect('index2.php?option=com_joomlacache&task=config');
}

function install_plugin() {
	global $database, $mosConfig_dbprefix, $mosConfig_absolute_path;

	$table = $mosConfig_dbprefix.'mambots';
	$path = $mosConfig_absolute_path.'/mambots/system/';

	$database->setQuery("select * from $table where element = 'joomlacache'");
	$database->loadObject($element);
	if(!$element) {
		$path_install = $mosConfig_absolute_path.'/components/com_joomlacache/plugin/';
		if(copy($path_install.'joomlacache.php', $path.'joomlacache.php')) {
			if(copy($path_install.'joomlacache.xm_', $path.'joomlacache.xml')) {
				$database->setQuery("insert into $table set name='JAccelerator Plugin', element='joomlacache', folder='system', published='1'");
				$database->query();
				return 1;
			}
		}
	}
	return 0;
}

function config() {
	global $mosConfig_absolute_path, $mosConfig_cachetime, $database;

	$params = '';
	$params['default_cache_time'] = $mosConfig_cachetime;

	$value = $params['default_cache_time'];
	$days = floor($value / 86400);
	$rest = $value - ($days * 86400);
	$hours = floor($rest / 3600);
	$rest = $rest - ($hours * 3600);
	$minutes = floor($rest / 60);
	$params['days'] = $days;
	$params['hours'] = $hours;
	$params['minutes'] = $minutes;

	$path = $mosConfig_absolute_path.'/components/com_joomlacache/config.php';
	if(file_exists($path)){
		include $path;

		$value = $params['default_cache_time'];
		$days = floor($value / 86400);
		$rest = $value - ($days * 86400);
		$hours = floor($rest / 3600);
		$rest = $rest - ($hours * 3600);
		$minutes = floor($rest / 60);
		$params['days'] = $days;
		$params['hours'] = $hours;
		$params['minutes'] = $minutes;
	}

	$database->setQuery("select * from #__mambots where element='joomlacache'");
	$database->loadObject($element);


	if($element){
		$params['published'] = $element->published;
	}else {
		$params['published'] = install_plugin();
	}
	HTML_joomlacache_admin::config($params);

}
function modules() {
	global $database, $mosConfig_absolute_path, $mosConfig_cachetime;

	$params['default_cache_time'] = $mosConfig_cachetime;
	$path_config = $mosConfig_absolute_path.'/components/com_joomlacache/config.php';
	if(file_exists($path_config))
		include $path_config;

	$path_config = $mosConfig_absolute_path.'/components/com_joomlacache/modules.php';
	if(file_exists($path_config)) {
		include $path_config;
	}

	$global = '';
	$global->size = 0;
	$global->hits = 0;
	$global->files = 0;

	$database->setQuery("select * from #__modules where access<'99'");
	$modules = $database->loadObjectList();

	for ($i = 0; $i < count($modules); $i++) {
		$id = 'id_'.$modules[$i]->id;
		if(isset($$id)) {
			$modules[$i]->cache = $$id->cache;

			foreach ($$id as $key => $value) {
				if($key == 'time') {
					$days = floor($value / 86400);
					$rest = $value - ($days * 86400);
					$hours = floor($rest / 3600);
					$rest = $rest - ($hours * 3600);
					$minutes = floor($rest / 60);
					$modules[$i]->days = $days;
					$modules[$i]->hours = $hours;
					$modules[$i]->minutes = $minutes;
				} else {
					$modules[$i]->$key = $value;
				}
			}
		} else {
			$value = $params['default_cache_time'];
			$days = floor($value / 86400);
			$rest = $value - ($days * 86400);
			$hours = floor($rest / 3600);
			$rest = $rest - ($hours * 3600);
			$minutes = floor($rest / 60);
			$modules[$i]->days = $days;
			$modules[$i]->hours = $hours;
			$modules[$i]->minutes = $minutes;
		}
		$modules[$i]->files = 0;
		$modules[$i]->size = 0;
		$modules[$i]->hits = 0;

		$path = $mosConfig_absolute_path.'/components/com_joomlacache/cache/modules/';

		$size = 0;
		$icount = 0;

		if(is_dir($path.$modules[$i]->id.'/files/')) {
			global $database;
			$moduleid = $modules[$i]->id;
			$database->setQuery("select sum(size) from #__joomlacache where element='$moduleid'");
			$size = $database->loadResult();

			$database->setQuery("select count(id) from #__joomlacache where element='$moduleid'");
			$icount = $database->loadResult();
		}

		$count = 0;
		if(file_exists($path.$modules[$i]->id.'/cache.log')) {
			include $path.$modules[$i]->id.'/cache.log';
		}

		$modules[$i]->size = $size;
		$modules[$i]->files = $icount;
		$modules[$i]->hits = $count;

		$global->size = $global->size + $size;
		$global->files = $global->files + $icount;
		$global->hits = $global->hits + $count;

	}

	HTML_joomlacache_admin::modules($modules, $global);
}

function components() {
	global $mosConfig_absolute_path, $database, $mosConfig_cachetime;
	$path = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/';
	$global = '';
	$global->size = 0;
	$global->hits = 0;
	$global->files = 0;
	$params['default_cache_time'] = $mosConfig_cachetime;
	$path_config = $mosConfig_absolute_path.'/components/com_joomlacache/config.php';
	if(file_exists($path_config)){
		include $path_config;
	}

	$database->setQuery("select * from #__components where link<>''");
	$components = $database->loadObjectList();

	$content->name = _JOOMLACACHE_CONTENT;
	$content->option = 'com_content';

	$count = 0;
	if(file_exists($path.'com_content/cache.log')) {
		include $path.'com_content/cache.log';
	}
	$content->hits = $count;

	$contents = array();
	$contents[] = $content;

	$allcomponents = array_merge($contents, $components);

	$path_config = $mosConfig_absolute_path.'/components/com_joomlacache/components.php';
	if(file_exists($path_config)) {
		include $path_config;
	}

	$i = 0;
	foreach ($allcomponents as $component) {
		$type = 0;
		$size = 0;
		$comp = $component->option;
		if(isset($$comp)) {
			$type = $$comp->cache;
		}
		$icount = 0;
		if(is_dir($path.$allcomponents[$i]->option.'/files/')) {
			global $database;
			$compname = $allcomponents[$i]->option;
			$database->setQuery("select sum(size) from #__joomlacache where element='$compname'");
			$size = $database->loadResult();

			$database->setQuery("select count(id) from #__joomlacache where element='$compname'");
			$icount = $database->loadResult();

		}
		$allcomponents[$i]->size = $size;
		$global->size = $global->size + $size;
		$allcomponents[$i]->files = $icount;
		$global->files = $global->files + $icount;

		$count = 0;
		if(file_exists($path.$component->option.'/cache.log')) {
			include $path.$component->option.'/cache.log';
		}

		$allcomponents[$i]->hits = $count;
		$global->hits = $global->hits + $count;
		$comp = $allcomponents[$i]->option;
		if(isset($$comp)) {
			foreach ($$comp as $key => $value) {
				if($key == 'time') {
					$days = floor($value / 86400);
					$rest = $value - ($days * 86400);
					$hours = floor($rest / 3600);
					$rest = $rest - ($hours * 3600);
					$minutes = floor($rest / 60);
					$allcomponents[$i]->days = $days;
					$allcomponents[$i]->hours = $hours;
					$allcomponents[$i]->minutes = $minutes;
				} else {
					$allcomponents[$i]->$key = $value;
				}
			}
		} else {
			$value = $params['default_cache_time'];

			$days = floor($value / 86400);
			$rest = $value - ($days * 86400);
			$hours = floor($rest / 3600);
			$rest = $rest - ($hours * 3600);
			$minutes = floor($rest / 60);
			$allcomponents[$i]->days = $days;
			$allcomponents[$i]->hours = $hours;
			$allcomponents[$i]->minutes = $minutes;
		}
		$i++;
	}
	HTML_joomlacache_admin::components($allcomponents, $global);
}

?>
